package org.jaaksi.demo.ptr.ui.base;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.jaaksi.demo.ptr.R;

/**
 * Created by Fcy on 2016/3/19.<br/>
 */
public class StateLayout extends FrameLayout {
  @IntDef({ NORMAL, NO_NET, SERVER_ERROR, NO_DATA, LOADING }) @Retention(RetentionPolicy.SOURCE)
  public @interface State {
  }

  public static final int NORMAL = 0;
  public static final int NO_NET = 1;
  public static final int SERVER_ERROR = 2;
  // 指list个数为0的
  public static final int NO_DATA = 3;
  public static final int LOADING = 4;
  /**
   * loading gone,ll_state_view不动
   */
  //public static final int STATE_OTHER = 7;

  // 不设置
  private static final int NULL = -1;

  @State private int state;

  private View ll_state_view;
  private FrameLayout loading_container;
  private View loading_view;
  private ImageView iv_hint;
  private TextView tv_hint, tv_hint_retry;

  @Nullable private View contentView;

  @DrawableRes private int imgNoNetId, imgNoDataId, imgSEId;
  @StringRes private int hintNoNetId, hintNoDataId, hintSEId;
  @StringRes private int hintRetryNoNetId, hintRetryNoDataId, hintRetrySEId;

  /*public StateView(Context context) {
    super(context);
    initView();
  }*/

  public StateLayout(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public StateLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    //initView();
  }

  @Override protected void onFinishInflate() {
    super.onFinishInflate();
    int childCount = getChildCount();
    if (childCount > 1) {
      throw new UnsupportedOperationException("most one child!");
    }
    contentView = getChildAt(0);
    // 这里不隐藏，使用方可以自己隐藏
    initView();
  }

  /**
   * 初始化资源
   */
  private void initRes() {
    imgNoNetId = R.drawable.img_no_net;
    imgSEId = imgNoDataId = R.drawable.img_no_data;
    hintNoNetId = R.string.str_no_net;
    hintSEId = hintNoDataId = R.string.str_server_error;
    hintRetryNoDataId = hintRetryNoNetId = hintRetrySEId = R.string.str_click_refresh;
  }

  private void initView() {
    View view = View.inflate(getContext(), R.layout.lib_statelayout, this);
    // 如果想自定义，可以添加一个容器。然后提供set方法,remove默认的
    loading_container = (FrameLayout) view.findViewById(R.id.loading_container);
    loading_view = loading_container.getChildAt(0);
    ll_state_view = view.findViewById(R.id.ll_state_view);
    iv_hint = (ImageView) ll_state_view.findViewById(R.id.iv_stateview_icon);
    tv_hint = (TextView) ll_state_view.findViewById(R.id.tv_stateview_hint);
    tv_hint_retry = (TextView) ll_state_view.findViewById(R.id.tv_stateview_retry);
    //addView(view, ViewGroup.LayoutParams.MATCH_PARENT,
    //        ViewGroup.LayoutParams.MATCH_PARENT);
    initRes();
    setState(NORMAL);
  }

  /**
   * 自定义loadingView
   */
  public void setLoadingView(View view) {
    if (view != loading_view) {
      loading_view = view;
      loading_container.removeAllViews();
      loading_container.addView(view);
    }
  }

  /**
   * 显示/隐藏contentView
   *
   * @param isShow 是否显示
   */
  public void showContentView(boolean isShow) {
    if (contentView != null) {
      if (isShow) {
        contentView.setVisibility(VISIBLE);
      } else {
        contentView.setVisibility(GONE);
      }
    }
  }

  public void initNoNetRes(int imgId, int hintId, int retryId) {
    imgNoNetId = imgId;
    hintNoNetId = hintId;
    hintRetryNoNetId = retryId;
  }

  public void initNoDataRes(int imgId, int hintId, int retryId) {
    imgNoDataId = imgId;
    hintNoDataId = hintId;
    hintRetryNoDataId = retryId;
  }

  public void initSERes(int imgId, int hintId, int retryId) {
    imgSEId = imgId;
    hintSEId = hintId;
    hintRetrySEId = retryId;
  }

  /*public void normal() {
    setState(NORMAL);
  }

  public void noNet() {
    setState(NO_NET);
  }

  public void noData() {
    setState(NO_DATA);
  }

  public void serverError() {
    setState(SERVER_ERROR);
  }*/

  @State public int getState() {
    return state;
  }

  public void setState(@State int state) {
    this.state = state;
    switch (state) {
      case NO_NET:
        showStateView(imgNoNetId, hintNoNetId, hintRetryNoNetId);
        break;
      case NO_DATA:
        showStateView(imgNoDataId, hintNoDataId, hintRetryNoDataId);
        break;
      case SERVER_ERROR:
        showStateView(imgSEId, hintSEId, hintRetrySEId);
        break;
      default:
        showStateView(NULL, NULL, NULL);
        break;
    }
  }

  private void showStateView(int imgId, int hintId, int hintRetryId) {

    if (state == NORMAL) {
      ll_state_view.setVisibility(GONE);
      loading_view.setVisibility(GONE);
    }
    /*else if (state == STATE_OTHER) {
      // ll_state_view.setVisibility(GONE);
      loading_view.setVisibility(GONE);
    }*/
    else if (state == LOADING) {
      ll_state_view.setVisibility(GONE);
      loading_view.setVisibility(VISIBLE);
    } else {
      loading_view.setVisibility(GONE);
      ll_state_view.setVisibility(VISIBLE);
      if (imgId != NULL) {
        iv_hint.setImageResource(imgId);
        iv_hint.setVisibility(VISIBLE);
      } else {
        iv_hint.setVisibility(GONE);
      }
      if (hintId == NULL) {
        tv_hint.setVisibility(GONE);
      } else {
        tv_hint.setText(hintId);
        tv_hint.setVisibility(VISIBLE);
      }
      if (hintRetryId == NULL) {
        tv_hint_retry.setVisibility(GONE);
      } else {
        tv_hint_retry.setText(hintRetryId);
        tv_hint_retry.setVisibility(VISIBLE);
      }
    }
  }

  public void setOnRetryListener(OnClickListener onRetryListener) {
    ll_state_view.setOnClickListener(onRetryListener);
  }
}
